package cn.virens.app.common;

import java.io.Serializable;

public class Time implements Serializable {
	private static final long serialVersionUID = 8016899198265292646L;

	private long time = 0;

	public Time() {
		this.time = System.currentTimeMillis();
		this.time = time / 1000; // 单位转为秒
		this.time = time + 28800; // 时区转换
		this.time = time % 86400; // 去掉天
	}

	public Time(int h) {
		this(h, 0);
	}

	public Time(int h, int m) {
		this(h, m, 0);
	}

	public Time(int h, int m, int s) {
		this.time += h * 3600;
		this.time += m * 60;
		this.time += s;
	}

	public long getTime() {
		return time;
	}

	public int getHour() {
		return (int) (time / 3600);
	}

	public int getMinute() {
		return (int) (time % 3600 / 60);
	}

	public int getSecond() {
		return (int) (time % 3600 % 60);
	}

	/**
	 * 在 when 之前
	 * 
	 * @param  when
	 * @return
	 */
	public boolean before(Time when) {
		return when == null ? false : time < when.time;
	}

	/**
	 * 在 when 之前 second 秒
	 * 
	 * @param  when
	 * @param  second
	 * @return
	 */
	public boolean before(Time when, int second) {
		return when == null ? false : time < when.time - second;
	}

	/**
	 * 在 when 之后
	 * 
	 * @param  when
	 * @return
	 */
	public boolean after(Time when) {
		return when == null ? false : time > when.time;
	}

	/**
	 * 在 when 之后 second 秒
	 * 
	 * @param  when
	 * @param  second
	 * @return
	 */
	public boolean after(Time when, int second) {
		return when == null ? false : time > when.time + second;
	}

	@Override
	public int hashCode() {
		return (int) time ^ (int) (time >> 32);
	}

	@Override
	public boolean equals(Object obj) {
		return obj instanceof Time && time == ((Time) obj).time;
	}

	@Override
	public String toString() {
		int h = getHour();
		int m = getMinute();
		int s = getSecond();

		StringBuilder sb = new StringBuilder();
		sb.append(h < 10 ? "0" : "").append(h);
		sb.append(m < 10 ? ":0" : ":").append(m);
		sb.append(s < 10 ? ":0" : ":").append(s);

		return sb.toString();
	}

}
